<?php

namespace App\Admin\Controllers;

use App\Models\City;
use App\Models\PickupSite;
use App\Models\PickupUser;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\MessageBag;

class PickupUserController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('核销人员');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('核销人员');
            $content->description('修改');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('核销人员');
            $content->description('新增');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(PickupUser::class, function (Grid $grid) {

            $grid->model()->orderBy('id', 'DESC');
            $grid->model()->where([['is_delete', '=', 0]]);
            $grid->id('ID')->sortable();
            $grid->pickupSite()->name('所属提货点');
            $grid->mobile('手机号');
            $grid->status('状态')->switch([
                'on'  => ['value' => 1, 'text' => '正常', 'color' => 'primary'],
                'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
            ]);
            $grid->is_pwd_reset('是否重置密码')->display(function ($is_pwd_reset) {
                return $is_pwd_reset ? "<span class=\"label label-primary\">是</span>" :"<span class=\"label label-danger\">否</span>";
            });
            $grid->created_at('创建时间');
            $grid->disableExport();
            $grid->disableExport();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
            });

            $grid->tools(function (Grid\Tools $tools) {
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
                    $actions->disableDelete();
                });
            });
            $grid->filter(function ($filter) {
                $filter->like('name', '名称');
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(PickupUser::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->hidden('id');
            $form->select('city_id', '选择城市')->options(function($id) {
                $city = City::where(['id' => $id])->first();
                if ($city) {
                    return [$city->id => $city->name];
                }
            })->ajax('/admin/api/cities')->load('site_id', '/admin/api/sites')->rules('required', [
                'required' => '所属城市不能为空'
            ]);;
            $form->select('site_id', '所属提货点')->rules('required', [
                'required' => '所属提货点不能为空'
            ]);
            $form->text("mobile", '手机号')->rules('required|min:11|max:11',[
                'required' => '手机号不能为空',
                'min' => '手机号长度为11位',
                'max' => '手机号长度为11位'
            ]);
            $form->switch('status', '状态')->states([
                'on'  => ['value' => 1, 'text' => '可见', 'color' => 'primary'],
                'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
            ])->default(1);
            $form->ignore(['city_id']);
            $form->saving(function (Form $form) {
                $id = $form->id;
                $where = [['mobile', '=', $form->mobile], ['is_delete', '=', 0]];
                if (!empty($id)) {
                    $where[] = ['id', '<>', $id];
                }
                $is_exist = PickupUser::where($where)->first();
                if (!empty($is_exist)) {
                    $error = new MessageBag([
                        'title'   => '错误',
                        'message' => '手机号已存在',
                    ]);
                    return back()->with(compact('error'));
                }
            });
        });
    }
}
